package com.xsimas.xyybackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsimas.xyybackend.domain.Brand;
import com.xsimas.xyybackend.dto.PageRequestDTO;
import com.xsimas.xyybackend.pojo.Result;
import com.xsimas.xyybackend.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RAY_HUI
 * @date 2025-07-09
 * @time 18:30
 */

@RestController
@RequestMapping("/api/brand")
@Tag(name = "品牌管理接口", description = "提供品牌的增删查改及分页操作")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询品牌数据", description = "根据页码和每页数量分页查询品牌信息")
    public Result<IPage<Brand>> getBrandsByPage(@RequestBody PageRequestDTO pageRequest) {
        Page<Brand> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.ok(brandService.page(page));
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有品牌列表", description = "返回所有品牌记录")
    public Result<List<Brand>> getAllBrands() {
        return Result.ok(brandService.list());
    }

    /**
     * 根据ID查询品牌
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单个品牌", description = "通过ID查询品牌详情")
    public Result<Brand> getBrandById(
            @Parameter(description = "品牌ID", required = true)
            @PathVariable Long id) {
        return Result.ok(brandService.getById(id));
    }

    /**
     * 新增品牌
     */
    @PostMapping("/add")
    @Operation(summary = "新增一个品牌记录", description = "将传入的品牌对象保存到数据库")
    public Result<Boolean> createBrand(
            @Parameter(description = "品牌实体对象", required = true)
            @RequestBody Brand brand) {
        boolean success = brandService.save(brand);
        return success ? Result.ok(success) : Result.error(400, "新增品牌失败");
    }

    /**
     * 更新品牌
     */
    @PutMapping("/update")
    @Operation(summary = "更新一个品牌记录", description = "根据ID更新品牌信息")
    public Result<Boolean> updateBrand(
            @Parameter(description = "品牌实体对象", required = true)
            @RequestBody Brand brand) {
        boolean success = brandService.updateById(brand);
        return success ? Result.ok(success) : Result.error(400, "更新品牌失败");
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除一个品牌", description = "逻辑或物理删除指定ID的品牌")
    public Result<Boolean> deleteBrand(
            @Parameter(description = "品牌ID", required = true)
            @PathVariable Long id) {
        boolean success = brandService.removeById(id);
        return success ? Result.ok(success) : Result.error(400, "删除品牌失败");
    }
}